﻿/*****************************************************************//**
  * @file   VAOneXMLAdaptorConstraints.h
  * @brief  VAOne读写连接器适配器
  *
  * @author lilongyuan (lilongyuan@diso.cn)
  * @date   2025-01-22
  *********************************************************************/
#ifndef VAONEXMLADAPTORCONSTRAINTS_H
#define VAONEXMLADAPTORCONSTRAINTS_H

#include "FITKAbstractVAOneXMLAdaptor.h"

namespace IO
{
    /**
     * @brief  XML读写适配器，用于读写载荷的约束数据
     * @author lilongyuan (lilongyuan@diso.cn)
     * @date   2025-01-22
     */
    class FITKVAOneXMLIOAPI VAOneXMLAdaptorConstraints : public FITKAbstractVAOneXMLAdaptor
    {
        FITKCLASS(IO, VAOneXMLAdaptorConstraints);
    public:
        /**
         * @brief     读取共有数据模板
         * @param[i]  node Constraints节点的子节点，data 载荷的约束数据对象
         * @return    是否执行成功
         * @author    lilongyuan (lilongyuan@diso.cn)
         * @date      2025-02-05
         */
        template <typename T>
        bool readCommonData(rapidxml::xml_node<char>* node, T data)
        {
            // 节点和数据对象不能为空
            if (node == nullptr || data == nullptr) return false;

            // 初始化读取状态
            bool ok = true;

            // 启用状态
            QString enabled = readStrAttribute(node, "enabled", &ok);
            if (ok) data->setEnabledState(enabled.toLower() == "true");

            // 名称
            QString plateEnergyName = getChildElementText(node, "Name");
            // 添加到哈希表里
            ok &= setIdToHash(VAOne::FITKVAOneXMLHash::Node_Constraints, plateEnergyName, data->getDataObjectID());
            // 设置名称
            data->setDataObjectName(getLoadManager()->checkName(plateEnergyName));

            // 注释
            QString note = getChildElementText(node, "Note");
            data->setNotes(note);

            return ok;
        }

        /**
         * @brief   写出名称和注释节点模板
         * @param   node父节点，data载荷数据对象
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        template <typename T>
        bool writeNameAndNoteNode(rapidxml::xml_node<char>* node, T data)
        {
            // 父节点
            if (node == nullptr) return false;

            // 写入名称
            rapidxml::xml_node<char>* name = createNodeElement(node, "Name", data->getDataObjectName());
            if (nullptr == name) return false;

            // 写入注释
            rapidxml::xml_node<char>* note = createNodeElement(node, "Note", data->getNotes());
            if (nullptr == note) return false;

            return true;
        }

        /**
         * @brief   构造函数
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        explicit VAOneXMLAdaptorConstraints() = default;

        /**
         * @brief   析构函数
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        ~VAOneXMLAdaptorConstraints() = default;

        /**
         * @brief   适配器读数据
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool adaptR() override;

        /**
         * @brief   适配器写数据
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool adaptW() override;

    private:
        /**
         * @brief   读取载荷的SEA梁约束数据
         * @param   node BeamUDEnergy节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool readBeamUDEnergy(rapidxml::xml_node<char>* node);

        /**
         * @brief   读取载荷的SEA板约束数据
         * @param   node PlateUDEnergy节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool readPlateUDEnergy(rapidxml::xml_node<char>* node);

        /**
         * @brief   读取载荷的SEA声腔约束数据
         * @param   node AcousticUDEnergy节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool readAcousticUDEnergy(rapidxml::xml_node<char>* node);

        /**
         * @brief   写入载荷的SEA梁约束数据
         * @param   node Constraints节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool writeBeamUDEnergy(rapidxml::xml_node<char>* node);

        /**
         * @brief   写入载荷的SEA板约束数据
         * @param   node Constraints节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool writePlateUDEnergy(rapidxml::xml_node<char>* node);

        /**
         * @brief   写入载荷的SEA声腔约束数据
         * @param   node Constraints节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool writeAcousticUDEnergy(rapidxml::xml_node<char>* node);

    private:
        /**
         * @brief   写出频谱数据
         * @param   node节点，spectrumName频谱名称，spectrumId频谱ID
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-22
         */
        bool writeSpectrumData(rapidxml::xml_node<char>* node, QString spectrumName, int spectrumId);
    };
    Register2FITKIOAdaptorFactory(VAOneXML, Constraints, VAOneXMLAdaptorConstraints)
}

#endif // VAONEXMLADAPTORCONSTRAINTS_H


